/**
 * 自定义函数
 * */


var formArys = {
    name: '', //场景名称
    view: '' //页面名称
}; //表单数据


//场景列表
function getIndex(){
    var formData = {
        scene_id: scene_id,
        pageStart: pages.start,
        pageLimit: pages.limit
    };
    console.log(formData);
    $.ajax({
        type: apis.sceneCntIndex.method,
        url: apis.sceneCntIndex.url,
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code != 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            formList = res.data;
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//场景列表字符串
function getHtml(d){
    var h = '', i, type;
    h += '';
    for (i = 0; i < d.length; i ++) {
        type = d[i].type;
        h += '<tr class="text-c">';
        h += '<td>' + d[i].id + '</td>';
        h += '<td>' + d[i].type_name + '</td>';
        h += '<td>';
        if (type > 1 && d[i].cnt != '') {
            if (type == 2) {
                h += '<img src="/' + d[i].cnt + '" width="50"/>';
            }
            if (type == 3) {
                h += '<video width="160" height="120" class="videoContent" controls="">';
                h += '<source src="/' + d[i].cnt + '" type="video/mp4">';
                h += '</video>';
            }
        } else {
            h += d[i].cnt;
        }
        h += '</td>';
        h += '<td class="td-manage">';
        //h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'scene-edit.html\', ' + d[i].id + ');">';
        //h += '编辑';
        ////h += '<i class="Hui-iconfont">&#xe6df;</i>';
        //h += '</a>';
        //h += ' &nbsp; ';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += ' 删除';
        h += '</a>';
        h += '<input type="hidden" id="title' + d[i].id + '" value="' + d[i].title + '"/>';
        h += '<input type="hidden" id="view' + d[i].id + '" value="' + d[i].view + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    var tt = $('#title' + id).val();
    var view = $('#view' + id).val();
    var thumb = $('#thumb' + id).val();
    tt = tt ? escape(tt) : '';
    url += '&title=' + tt;
    url += '&view=' + view;
    url += '&thumb=' + thumb;
    layer_show(title,url,w,h);
}


//抓图
function uploadImg() {
    var fileObj = $('#imgUpload')[0], reader = new FileReader();
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (res){
        var formData = {
            img: res.target.result,
        };
        //console.log(formData);
        //return
        $.ajax({
            type: apis.sceneCntUploadImg.method,
            url: apis.sceneCntUploadImg.url,
            data: formData,
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    layer.msg(res.msg,{icon: 1,time: 1000});
                    return
                }
                //msgPop({msg: '上传成功'});
                layer.msg('上传成功',{icon: 1,time: 1000});
                //setTimeout(function(){
                //    //top.location.reload();
                //    location.reload();
                //}, 1000);
                var h = '';
                h += '<img src="/' + res.data + '"/>';
                h += '<input type="hidden" id="img" value="' + res.data + '"/>';
                $('#imgShow').html(h);
            },
            error: function () {
                console.log('错误啦！');
            }
        });
    }
}


//视频上传
function uploadSp(){
    var uploadFile = $("#videoUpload")[0].files[0];
    var formData = new FormData();
    formData.append("file", uploadFile);
    $.ajax({
        type: apis.sceneCntUploadVideo.method,
        url: apis.sceneCntUploadVideo.url,
        data: formData,
        async: false,
        // 下面这两个参数
        processData: false,
        contentType: false,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                layer.msg(res.msg,{icon: 1,time: 1000});
                return
            }
            //msgPop({msg: '操作成功'});
            layer.msg('上传成功',{icon: 1,time: 1000});
            //setTimeout(function(){
            //    //top.location.reload();
            //    location.reload();
            //}, 1000);
            var h = '';
            h += '<video width="320" height="240" class="videoContent" controls="" poster="">';
            h += '<source src="/' + res.data + '" type="video/mp4">';
            h += '</video>';
            h += '<input type="hidden" id="video" value="' + res.data + '"/>';
            $('#videoShow').html(h);
        },
        error: function () {
            console.log('错误啦！');
        }
    });
}


////获取详情
//function getOne(){
//    $('#title')[0].value = title;
//    $('#view')[0].value = view;
//    var h = '';
//    if (thumb != '') {
//        h += '<img src="/' + thumb + '"/>';
//        h += '<input type="hidden" id="img" value="' + thumb + '"/>';
//    }
//    $('#bigShow').html(h);
//}


////显示图片
//function showImg(e){
//    var h = '';
//    h += '<img src="' + e + '" style="width: 150px;"/>';
//    h += '<input type="hidden" id="imgLink" value="' + e + '"/>';
//    $('#img').html(h);
//}


//修改
function modify(){
    var formData = {
        id: id,
        scene_id: scene_id,
        type: $('#type').val(),
        title: $('#title').val(),
        img: $('#img').val(),
        video: $('#video').val(),
    };
    if (formData.scene_id == '') {
        msgPop({msg: '场景必须的'}); return
    }
    if (formData.type == '') {
        msgPop({msg: '类型必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apis.sceneCntModify.method,
        url: apis.sceneCntModify.url,
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code != 0) {
                //msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                //top.location.reload();
                window.location.href = '/admin/view-ewm/scene-cnt.html?scene_id=' + scene_id;
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apis.sceneCntDel.method,
            url: apis.sceneCntDel.url,
            data: { id: id},
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 500});
                setTimeout(function () {
                    //top.location.reload();
                    window.location.href = '/admin/view-ewm/scene-cnt.html?scene_id=' + scene_id;
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}